<?php
class TiensController extends AppController {

	var $name = 'Tiens';

	function index($idct) {
		$this->Tien->recursive = 0;
		$this->set('tiens', $this->paginate('Tien',array('ctrinh_id'=>$idct)));
		$this->set(compact('idct'));
	}

	
	function add($idct) {
		if (!empty($this->data)) {
			$this->Tien->create();
			if ($this->Tien->save($this->data)) {
				$this->Session->setFlash(__('The tien has been saved', true));
				$this->redirect(array('action' => 'index',$idct));
			} else {
				$this->Session->setFlash(__('The tien could not be saved. Please, try again.', true));
			}
		}
		
		$idvon_daco=$this->Tien->find('list',array('conditions'=>array('ctrinh_id'=>$idct),
			'fields'=>array('nguonvon_id','nguonvon_id')));
		$nguonvons = $this->Tien->Nguonvon->find('list',array(
			'fields'=>array('id','tennv'),'conditions'=>array('NOT'=>array('id'=>$idvon_daco))));
		$this->set(compact('ctrinhs', 'nguonvons','idct'));
	}

	function edit($id = null,$idct) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid tien', true));
			$this->redirect(array('action' => 'index',$idct));
		}
		if (!empty($this->data)) {
			if ($this->Tien->save($this->data)) {
				$this->Session->setFlash(__('The tien has been saved', true));
				$this->redirect(array('action' => 'index',$idct));
			} else {
				$this->Session->setFlash(__('The tien could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Tien->read(null, $id);
		}
		
		$idvon_daco=$this->Tien->find('list',array('conditions'=>array('ctrinh_id'=>$idct),
			'fields'=>array('nguonvon_id','nguonvon_id')));
		$nguonvons = $this->Tien->Nguonvon->find('list',array(
			'fields'=>array('id','tennv'),'conditions'=>array('NOT'=>array('id'=>$idvon_daco))));
		$this->set(compact('ctrinhs', 'nguonvons','idct','id'));
	}

	function delete($id = null,$idct) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for tien', true));
			$this->redirect(array('action'=>'index',$idct));
		}
		if ($this->Tien->delete($id)) {
			$this->Session->setFlash(__('Tien deleted', true));
			$this->redirect(array('action'=>'index',$idct));
		}
		$this->Session->setFlash(__('Tien was not deleted', true));
		$this->redirect(array('action' => 'index',$idct));
	}
}
